<?php


namespace app\common\service;


use app\common\model\Registered;

class SignService
{
    private $d_status_arr;
    private $f_status_arr;
    private $d_time_arr;
    private $f_time_arr;
    private $timestamp;

    /**
     * 获取全部状态的数组，组成一个新的数组，每个数组的值都为0，下表为元数组的下标
     * 获取本月，本周，昨天，今天的时间戳
     * SignService constructor.
     */
    public function __construct()
    {
        $this->setArr();
        $this->setTime();
    }

    private function setArr()
    {
        $arr_d = array();
        foreach (config('status.d') as $key => $value){
            $arr_d[$key] = 0;
        }
        $this->d_status_arr = $arr_d;

        $arr_f = array();
        foreach (config('status.f') as $key => $value){
            $arr_f[$key] = 0;
        }
        $this->f_status_arr = $arr_f;
    }
    private function setTime()
    {
        $this->timestamp = [
            'month_start' => strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y')))),
            'month_end' => strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y')))),
            'week_start' => mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')),
            'week_end' => mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y')),
            'yesterday_start' => mktime(0,0,0,date('m'),date('d')-1,date('Y')),
            'yesterday_end' => mktime(0,0,0,date('m'),date('d'),date('Y'))-1,
            'today_start' => mktime(0,0,0,date('m'),date('d'),date('Y')),
            'today_end' => mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1
        ];
        $this->d_time_arr = [
            'today' => 0,
            'week' => 0,
            'yesterday' => 0,
            'month' => 0,
        ];
        $this->f_time_arr = [
            'today' => 0,
            'week' => 0,
            'yesterday' => 0,
            'month' => 0,
        ];
    }

    public function signStatistics($sign, $arr)
    {
        $this->everySign($arr);
        return [
            'd_status_arr' => $this->d_status_arr,
            'f_status_arr' => $this->f_status_arr,
            'd_time_arr' => $this->d_time_arr,
            'f_time_arr' => $this->f_time_arr,
            'sign' => $sign
        ];
    }

    /**
     * 循环遍历每一个标识的数据
     * @param $data
     */
    private function everySign($data)
    {
        foreach ($data as $key => $list){
            if($list['type'] == 'd'){
                $this->d_sign_list($list);
            }
            if($list['type'] == 'f'){
                $this->f_sign_list($list);
            }
        }
    }

    /**
     * 处理单期的数据
     * @param $list 每一条数据
     */
    private function d_sign_list($list)
    {
        $time = strtotime($list['registered_time']);
        $this->d_status_arr[$list['loan_status']] += 1;
        # 计算日期的
        $this->registered_time($time, $list['type']);
    }
    /**
     * 处理分期的数据
     * @param $list 每一条数据
     */
    private function f_sign_list($list)
    {
        $time = strtotime($list['registered_time']);
        $this->f_status_arr[$list['loan_status']] += 1;
        # 计算日期的
        $this->registered_time($time, $list['type']);
    }
    # 通过注册时间来判断是属于哪个阶段的
    private function registered_time($registered_time, $type)
    {
        if($this->timestamp['today_start'] < $registered_time && $registered_time< $this->timestamp['today_end']){
            $type == 'f' ? $this->f_time_arr['today'] += 1 : $this->d_time_arr['today'] += 1;
        }
        if($this->timestamp['yesterday_start'] < $registered_time && $registered_time< $this->timestamp['yesterday_end']){
            $type == 'f' ? $this->f_time_arr['yesterday'] += 1 : $this->d_time_arr['yesterday'] += 1;
        }
        if($this->timestamp['week_start'] < $registered_time && $registered_time< $this->timestamp['week_end']){
            $type == 'f' ? $this->f_time_arr['week'] += 1 : $this->d_time_arr['week'] += 1;
        }
        if($this->timestamp['month_start'] < $registered_time && $registered_time< $this->timestamp['month_end']){
            $type == 'f' ? $this->f_time_arr['month'] += 1 : $this->d_time_arr['month'] += 1;
        }
    }
}